package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.pojo.Resources;
import com.entity.vo.ResourceVo;
import com.mapper.ResourcesMapper;
import com.service.ResourcesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenMo
 * @since 2022-04-21
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements ResourcesService {

    @Override
    public List<ResourceVo> pageResourcesCondition(long current, long limit, ResourceVo resourceQuery) {
        Page<ResourceVo> pageCondition = new Page<>(current, limit);
        QueryWrapper<Resources> wrapper = new QueryWrapper<>();
        validateCondition(wrapper, resourceQuery);
        List<ResourceVo> pageResourcesCondition = baseMapper.getPageResourcesCondition(pageCondition, wrapper);
        pageResourcesCondition.forEach(item -> {
            item.setConceptCodeAndName(item.getConceptCode() + "-" + item.getConceptName());
        });
        return pageResourcesCondition;
    }

    private void validateCondition(QueryWrapper<Resources> wrapper, ResourceVo resourceQuery) {
        String resourceContent = resourceQuery.getResourceContent();
        String propertiesKey = resourceQuery.getPropertiesKey();
        String propertiesValue = resourceQuery.getPropertiesValue();
        Integer isDeleted = resourceQuery.getIsDeleted();

        if (StringUtils.hasText(resourceContent)) {
            wrapper.like("resources.resourceContent", resourceContent);
        }
        if (StringUtils.hasText(propertiesKey)) {
            wrapper.like("resources.propertiesKey", propertiesKey);
        }
        if (StringUtils.hasText(propertiesValue)) {
            wrapper.like("resources.propertiesValue", propertiesValue);
        }
        if (isDeleted != null) {
            wrapper.eq("resources.isDeleted", 0);
        }
    }
}
